<?php
namespace Admin\Controller;
use Admin\Controller\ApplyController;
/**
 * 保险管理
 */
class InsuranceController extends AdminController
{
	public function setStatus()
	{
		$status = I('get.status', 0, 'intval');
		$result = M('insurance_company')->where('id=' . I('get.id', 0, 'intval'))->setField('status', ($status == 1 ? 0 : 1));
		$this->returnResult($result);
	}
	//保险详情首页
	public function index(){
        $where = '1 = 1';
	    if(I('get.aid', -1, 'intval') <> -1)
	    {
	      if(I('get.aid', -1, 'intval')==0){
	        $where .=' and  u.aid =0';
	      }else{
	        $where .=' and aid='.I('get.aid', 0, 'intval');
	      }
	    }
	    $where .= I('get.user_name') != '' ? ' and u.user_name like "%'.I('get.user_name').'%"' : '';

	    $where .= I('get.telephone') != '' ? ' and u.telephone = '.I('get.telephone') : '';

	    $where .= I('get.create_time_beg') != '' ? ' and u.create_time >= '.strtotime(I('get.create_time_beg')) : '';

	    $where .= I('get.create_time_end') != '' ? ' and u.create_time < '.strtotime(I('get.create_time_end')) : '';
	    $where.=' and u.id=m.user_id and u.status=10 and m.status >= 3';
  //       if(session('adminInfo.gpid')!=0){
  //            $where.=' and gpid='.session('adminInfo.gpid');
		// }
		$list=$this->getList('m.id as mat_id,m.bank_card,m.bank_name,m.pay_id,m.add_time,m.status as mat_status,u.*',array('user u','advance m'),$where,'id desc',true);

		foreach ($list as $key => $value) {
			 $group=$this->getInfo('*','group','id='.$value['gpid']);
             $list[$key]['group_name']=$group['name'];
		}
		 $this->assign('list',$list);
		 $this->display();
	}


//添加/修改保险详情
	public function edit(){
		if(IS_POST){
			if($_POST['type_status']==1){
			  $insurance['business_cid']=$_POST['business_id'];
              $insurance['business_company']=$this->get_company_name($_POST['business_id']);
              $insurance['business_warranty']=$_POST['business_warranty'];
              $insurance['business_create_time']=strtotime($_POST['business_create_time']);
              $insurance['business_end_time']=strtotime($_POST['business_end_time']);
              $insurance['business_status']=1;
			}else{
			  $insurance['compulsory_cid']=$_POST['compulsory_id'];
			  $insurance['compulsory_company']=$this->get_company_name($_POST['compulsory_id']);
              $insurance['compulsory_warranty']=$_POST['compulsory_warranty'];
              $insurance['compu_create_time']=strtotime($_POST['compu_create_time']);
              $insurance['compu_end_time']=strtotime($_POST['compu_end_time']);
              $insurance['compulsory_status']=1;
			}
			$insurance['user_id']=$_POST['user_id'];
			$insurance['admin_name']=session('adminInfo.name');
            $insurance['year']=$_POST['year'];//年度
            $insurance['type']=$_POST['type'];//1：新保:2：续保
			$insurance['car_lose_insurance']=$_POST['car_lose_insurance'];
			$insurance['third_liability_insurance']=$_POST['third_liability_insurance'];
			$insurance['passenger_insurance']=$_POST['passenger_insurance'];
			$insurance['full_car_theft']=$_POST['full_car_theft'];
			$insurance['self_combustion']=$_POST['self_combustion'];
			$insurance['exclude_franchise_insurance']=$_POST['exclude_franchise_insurance'];
			$insurance['glass_breakage_insurance']=$_POST['glass_breakage_insurance'];
			$insurance['compulsory_insurance']=$_POST['compulsory_insurance'];
			$insurance['car_scratch_insurance']=$_POST['car_scratch_insurance'];
			$insurance['transport_insurance']=$_POST['transport_insurance'];
			$insurance['total_fee']=$_POST['total_fee'];
			$insurance['issue_time']=strtotime($_POST['issue_time']);
			$insurance['status']=1;

			$detail_info=$this->getInfo('*','insurance','status=1 and user_id='.$_POST['user_id'].' and year='.$_POST['year']);
			if($detail_info){
				if($detail_info['compulsory_status']==0 || $detail_info['business_status']==0){
                    $result=M('insurance')->where('status=1 and user_id='.$_POST['user_id'].' and year='.$_POST['year'])->save($insurance);
				}else{
					$this->error('交强险或商业险都已确认,不能修改',U('Insurance/index'),0);
				}
			}else{
				$result=M('insurance')->add($insurance);
			}

			if($result){
				redirect('/Admin/Insurance/index');

			}else{
				$this->error('操作失败',U('Insurance/index'),0);
			}

		}else{
			$user=$this->getInfo('*','user','id='.I('get.user_id',0,'intval'));
			$info=$this->getInfo('*','insurance','user_id='.I('get.user_id',0,'intval').' and year=1');
			$info2=$this->getInfo('*','insurance','user_id='.I('get.user_id',0,'intval').' and year=2');
			$info3=$this->getInfo('*','insurance','user_id='.I('get.user_id',0,'intval').' and year=3');
			$insurance_company=$this->getList('*','insurance_company','status=1');
            $this->assign('user',$user);
			$this->assign('info',$info);
			$this->assign('info2',$info2);
			$this->assign('info3',$info3);
			$this->assign('company_list',$insurance_company);
			$this->display('add_detail');
		}
	}

	//保险公司管理
	public function company_index(){
		$list=$this->getList('*','insurance_company','1=1','sort desc,id asc',true);
		$this->assign('list',$list);
		$this->display();
	}
//添加保险公司
	public function company_add(){
		if(IS_POST){
            $data=array(
              'company_name'=>$_POST['company_name'],
              'sort'=>$_POST['sort'],
              'add_time'=>time(),
              'status'=>$_POST['status'],
              );
            $result = $this->updateData($data, 'insurance_company');
			//$this->returnResult($result);
			$this->returnResult($result, null, U('Insurance/company_index'));
		}else{
			$this->display();
		}

	}
//修改保险公司
	public function company_edit(){
		if(IS_POST){
            $data=array(
              'company_name'=>$_POST['company_name'],
              'sort'=>$_POST['sort'],
              'add_time'=>time(),
              'status'=>$_POST['status'],
              );
            $result = M('insurance_company')->where('id='.$_POST['id'])->save($data);
			//$this->returnResult($result);
			$this->returnResult($result, null, U('Insurance/company_index'));
		}else{
			$info=$this->getInfo('*','insurance_company','id='.I('get.id',0,'intval'));
			$this->assign('info',$info);
			$this->display('company_add');
		}
	}
	// 删除保险详情
	 public function del(){
     	$id=I('get.id',0,'intval');
		$result = $this->deleteData('id=' . $id, 'insurance');
		$this->returnResult($result);
     }
//删除保险公司
     public function company_del(){
     	$id=I('get.id',0,'intval');
		$result = $this->deleteData('id=' . $id, 'insurance_company');
		$this->returnResult($result);
     }



     //获得保险公司
     public function get_company_name($company_id){
        $info=$this->getInfo('*','insurance_company','id='.$company_id);
        return $info['company_name'];
     }




}